#pragma once

#include "noncopyable.h"
#include "Thread.h"

#include <functional>
#include <mutex>
#include <condition_variable>
#include <string>

class EventLoop;

class EventLoopThread : noncopyable
{
public:
    using ThreadInitCallback = std::function<void(EventLoop *)>;

    EventLoopThread(const ThreadInitCallback &cb = ThreadInitCallback(), const std::string &name = std::string());
    ~EventLoopThread();

    EventLoop *startLoop();

private:
    void threadFunc();

    EventLoop *loop_;  // 将在新线程中指向创建的EventLoop对象
    bool exiting_;  // 标记是否退出
    Thread thread_;  // 创建底层线程对象，绑定线程的入口函数为当前类的成员函数threadFunc,并指定线程名称
    std::mutex mutex_;
    std::condition_variable cond_;
    ThreadInitCallback callback_;  // 保存用户传入的初始化回调，用于在EventLoop启动前做额外配置
};